import { TStringHelper } from "@/baselib/aPublic/StringHelper"
import { TMenuItem, TRoleRightItem } from "@/baselib/right/BaseRightClass";
export interface IElmTreeData {
    id: string;
    value: string;
    label: string;
    children: IElmTreeData[];
}
const elmHelper = {
    //[{id,pid,..}]-转化成elm
    dataToTree: function (QDatas: any, QKeyField: string, QPKeyField: string, QLableField: string, QCodeField: string) {
        let lTrees: any[] = [];
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            var keyValue = qJson[QKeyField];
            var pkeyValue = qJson[QPKeyField];
            var lableValue = qJson[QLableField];
            var lCodeValue = qJson[QCodeField];
            if (TStringHelper.stringIsEmpty(lableValue)) {
                lableValue = "标签";
            }
            if (!TStringHelper.stringIsEmpty(lCodeValue)) {
                lableValue = lableValue + '-' + lCodeValue;
            }
            var itemTree = { id: keyValue, label: lableValue, children: [] };
            var bchild = false;

            if (!TStringHelper.stringIsEmpty(pkeyValue)) {
                for (var i = 0; i < lTrees.length; i++) {
                    var pTree = lTrees[i];
                    if (pTree.id == pkeyValue) {
                        pTree.children.push(itemTree);
                        bchild = true;
                        break;
                    }
                }
            }

            if (!bchild) {
                lTrees.push(itemTree);
            }
        });
        return lTrees;
    },
    //[{id,pid,..}]-转化成elm Tree 树形控件 或 Cascader 级联选择器（多了个value字段)
    dataToElmTree: function (QDatas: any, QKeyField: string, QPKeyField: string, QLableField: string, QValueField: string) {
        let lElmTree: any[] = [];
        let lDictChilds: { [index: string]: number; } = {};
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            let lPKeyValue = qJson[QPKeyField];
            if (lPKeyValue in lDictChilds) {
                lDictChilds[lPKeyValue] = lDictChilds[lPKeyValue] + 1;
            } else {
                lDictChilds[lPKeyValue] = 1;
            }
        });
        let lDictItems: { [index: string]: any; } = {};
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            let lKeyValue = qJson[QKeyField];
            let lPKeyValue = qJson[QPKeyField];
            let lLable = qJson[QLableField];
            let lValue = qJson[QValueField];
            if (lValue == null || lValue == undefined) {
                lValue = lKeyValue;
            }
            let lHaveChild = false;
            if (lKeyValue in lDictChilds) {
                lHaveChild = (lDictChilds[lKeyValue] >= 1);
            }
            //
            let itemTree = { id: lKeyValue, label: lLable, value: lValue, children: [] };
            let itemTreeB = { id: lKeyValue, label: lLable, value: lValue, };
            if (lHaveChild) {
                lDictItems[lKeyValue] = itemTree;
            } else {
                lDictItems[lKeyValue] = itemTreeB;
            }
            if (lPKeyValue in lDictItems) {
                if (lHaveChild) {
                    lDictItems[lPKeyValue].children.push(itemTree);
                } else {
                    lDictItems[lPKeyValue].children.push(itemTreeB);
                }
            } else {
                if (lHaveChild) {
                    lElmTree.push(itemTree);
                } else {
                    lElmTree.push(itemTreeB);
                }
            }
        });
        return lElmTree;
    },
    //[{id,chidler:[]}]
    dataTreeToElmTree: function (QDatas: any, QKeyField: string, QChildrenField: string, QLableField: string, QValueField: string): IElmTreeData[] {
        let lElmTree: any[] = [];
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            var lKeyValue = qJson[QKeyField];
            var lLable = qJson[QLableField];
            var lValue = qJson[QValueField];
            var lChildren = qJson[QChildrenField];
            if ((lChildren as []).length > 0) {
                let itemTree = { id: lKeyValue, label: lLable, value: lValue, children: [] };
                lElmTree.push(itemTree);
                itemTree.children = elmHelper.dataTreeToElmTree(lChildren, QKeyField, QChildrenField, QLableField, QValueField) as [];
            } else {
                let itemTreeB = { id: lKeyValue, label: lLable, value: lValue };
                lElmTree.push(itemTreeB);
            }
        });
        return lElmTree;
    },

    //[{id,pid,..}] 加上children属性 ，供table树形展示用的
    dataAddchildren: function (QDatas: any, QKeyField: string, QPKeyField: string) {
        let lElmTree: any[] = [];
        let lDictChilds: { [index: string]: number; } = {};
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            let lPKeyValue = qJson[QPKeyField];
            if (lPKeyValue in lDictChilds) {
                lDictChilds[lPKeyValue] = lDictChilds[lPKeyValue] + 1;
            } else {
                lDictChilds[lPKeyValue] = 1;
            }
        });
        let lDictItems: { [index: string]: any; } = {};
        QDatas.forEach(function (qJson: { [x: string]: any; }) {
            let lKeyValue = qJson[QKeyField];
            let lPKeyValue = qJson[QPKeyField];
            let lHaveChild = false;
            if (lKeyValue in lDictChilds) {
                lHaveChild = (lDictChilds[lKeyValue] >= 1);
            }
            let temp: any = {};
            //新的对象
            temp = JSON.parse(JSON.stringify(qJson));
            if (lHaveChild) {
                temp.children = [];

            }
            lDictItems[lKeyValue] = temp;
            if (lPKeyValue in lDictItems) {
                lDictItems[lPKeyValue].children.push(temp);
            } else {
                lElmTree.push(temp);
            }
        });
        return lElmTree;
    },
    //
    menuListGetCheckKeys: function (QMenus: TMenuItem[]): string[] {
        let lResult: string[] = [];
        QMenus.forEach(itemMenu => {
            if (itemMenu.menuStatus == "启用") {
                lResult.push(itemMenu.menuIndexName);
            }

            if (itemMenu.menuChildren.length > 0) {
                let lChildren = elmHelper.menuListGetCheckKeys(itemMenu.menuChildren);
                lResult.push(...lChildren);
            }
        });
        return lResult;
    },
    //
    rightListGetCheckKeys: function (QRights: TRoleRightItem[]): string[] {
        let lResult: string[] = [];
        QRights.forEach(item => {
            if (item.rightStatus == "启用") {
                lResult.push(item.FRightID);
            }

            if (item.rightChildren.length > 0) {
                let lChildren = elmHelper.rightListGetCheckKeys(item.rightChildren);
                lResult.push(...lChildren);
            }
        });
        return lResult;
    },
}
export default elmHelper